package com.rainxing.cockpit.modules.quartz.job;

import com.rainxing.cockpit.modules.quartz.service.ScheduledService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Auther: ybzhu
 * @Date: 2018/7/19 13:43
 * @Description:
 */
@Component
@Slf4j
public class MyTask implements Job {
    //这里就可以通过spring注入bean了
    @Autowired
    private ScheduledService scheduledService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        boolean isExecute = false;  //是否已执行业务逻辑
        boolean flag = false;  //业务逻辑执行后返回结果
        log.info("===========================开始执行任务===========================");
        log.info("task号任务" + new Date());

        try {
            //可以通过context拿到执行当前任务的quartz中的很多信息，如当前是哪个trigger在执行该任务
            CronTrigger trigger = (CronTrigger) context.getTrigger();
            String corn = trigger.getCronExpression();
            String jobName = trigger.getKey().getName();
            String jobGroup = trigger.getKey().getGroup();
            log.info("corn:" + corn);
            log.info("jobName:" + jobName);
            log.info("jobGroup:" + jobGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("===========================执行任务结束===========================");
    }

}
